using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;

namespace PackTracker.Business
{ 
	[Serializable()] 
	public class UsuarioCollection : Csla.BusinessListBase<UsuarioCollection, Usuario>
	{
		#region BindingList Overrides
		protected override object AddNewCore()
		{
			Usuario item = Usuario.NewUsuario();
			this.Add(item);
			return item;
		}
		#endregion //BindingList Overrides

		#region Authorization Rules

		public static bool CanGetObject()
		{
			//TODO: Define CanGetObject permission in UsuarioCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("UsuarioCollectionViewGroup"))
			//	return true;
			//return false;
		}

		public static bool CanAddObject()
		{
			//TODO: Define CanAddObject permission in UsuarioCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("UsuarioCollectionAddGroup"))
			//	return true;
			//return false;
		}

		public static bool CanEditObject()
		{
			//TODO: Define CanEditObject permission in UsuarioCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("UsuarioCollectionEditGroup"))
			//	return true;
			//return false;
		}

		public static bool CanDeleteObject()
		{
			//TODO: Define CanDeleteObject permission in UsuarioCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("UsuarioCollectionDeleteGroup"))
			//	return true;
			//return false;
		}
		#endregion //Authorization Rules

		#region Factory Methods
		private UsuarioCollection()
		{ /* require use of factory method */ }

		private UsuarioCollection(SafeDataReader dr)
		{
			MarkAsChild();
			Fetch(dr);
		}

		public static UsuarioCollection NewUsuarioCollection()
		{
			if (!CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a UsuarioCollection");
			return new UsuarioCollection();
		}

		public static UsuarioCollection GetUsuarioCollection()
		{
			if (!CanGetObject())
				throw new System.Security.SecurityException("User not authorized to view a UsuarioCollection");
			return DataPortal.Fetch<UsuarioCollection>(new FilterCriteria());
		}

		internal static UsuarioCollection GetUsuarioCollection(SafeDataReader dr)
		{
			return new UsuarioCollection(dr);
		}


		#endregion //Factory Methods

		#region Data Access

		#region Filter Criteria
		[Serializable()]
		private class FilterCriteria
		{

			public FilterCriteria()
			{
			}

		}
		#endregion //Filter Criteria

		#region Data Access - Fetch
		private void DataPortal_Fetch(FilterCriteria criteria)
		{
			RaiseListChangedEvents = false;

			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				ExecuteFetch(cn, criteria);
			}//using

			RaiseListChangedEvents = true;
		}

		private void ExecuteFetch(SqlConnection cn, FilterCriteria criteria)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_GetUsuarioCollection";
				/*

				*/

				using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
				{
					Fetch (dr);
				}
			}//using
		}

		private void Fetch(SafeDataReader dr)
		{
			while (dr.Read())
				this.Add(Usuario.GetUsuario(dr));
		}

		#endregion //Data Access - Fetch

		#region Data Access - Update
		protected override void DataPortal_Update()
		{

			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				Update(cn);

			}//using SqlConnection
		}

		internal void Update(SqlConnection cn)
		{
			RaiseListChangedEvents = false;

			// loop through each deleted child object
			foreach (Usuario deletedChild in DeletedList)
				deletedChild.DeleteSelf(cn);
			DeletedList.Clear();

			// loop through each non-deleted child object
			foreach (Usuario child in this)
			{
				if (child.IsNew)
					child.Insert(cn);
				else
					child.Update(cn);
			}

			RaiseListChangedEvents = true;
		}

		#endregion //Data Access - Update
		#endregion //Data Access
	}
}
